package com.example.votesystem.controller;

import com.example.votesystem.constant.MessageConstant;
import com.example.votesystem.entity.PageResult;
import com.example.votesystem.entity.QueryPageBean;
import com.example.votesystem.entity.Result;
import com.example.votesystem.pojo.TItem;
import com.example.votesystem.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 检查项管理
 */

@RestController
@RequestMapping("/checkitem")
public class CheckItemController {
    @Autowired//查找服务
    private CheckItemService checkItemService;

    //新增检查项
    @PostMapping("/add")
    public Result add(@RequestParam(value = "id") Integer id,@RequestParam(value = "name") String name,@RequestParam(value = "hearUrl") String headUrl){
        try{
            checkItemService.add(id,name,headUrl);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_CHECKITEM_FAIL);
        }
        return  new Result(true, MessageConstant.ADD_CHECKITEM_SUCCESS);
    }

//    //检查项分页查询
//    @RequestMapping("/findPage")
//    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
//        PageResult pageResult = checkItemService.pageQuery(queryPageBean);
//        return pageResult;
//    }

    //删除检查项
    @PostMapping("/delete")
    public Result delete(@RequestBody Integer[] roleIdsss){
        try{
            checkItemService.delete(roleIdsss);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }
        return  new Result(true, MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    //编辑检查项
    @PostMapping("/update")
    public Result update(@RequestBody TItem tItem){
        try{
            checkItemService.update(tItem);
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return  new Result(true, MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }

//    @RequestMapping("/findById")
//    public Result findById(Integer id){
//        try{
//            CheckItem checkItem = checkItemService.findById(id);
//            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
//        }catch (Exception e){
//            e.printStackTrace();
//            //服务调用失败
//            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
//        }
//    }

    @RequestMapping("/findAll")
    public Result findAll(){
        try{
            List<TItem> list = checkItemService.findAll();
            return  new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,list);
        }catch (Exception e){
            e.printStackTrace();
            //服务调用失败
            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
        }
    }
}
